package com.AUI.components
{
	import com.AUI.*;
	
	import flash.display.*;
	import flash.events.*;

	/**
	 * Table body.
	 * @author HuangMinPing
	 * @site www.AminLab.com
	 */
	public class Table extends V
	{
		/**
		 * Column width.
		 */
		public var columnsWidth:Vector.<int> = new Vector.<int>;
		/**
		 * Constructor.
		 */
		public function Table()
		{
			// init attributes
			depth = 4;
			
			/**************** INITCOMPLETE ****************/
			Object(this).constructor == Table && initComplete();
		}
		/**
		 * Get or set a string array that indicates the lables of cells of the table.
		 */
		override public function set value(variable:*):void
		{
			setValue(variable,0);
		}
		override public function get value():*
		{
			var len:int = numElements;
			var items:Array = new Array(len);
			for(var i:int = 0;i < len;++i)
			{
				items[i] = AComponent(getElementAt(i)).value;
			}
			return items;
		}
		/**
		 * Get or set a string array that indicates the lables of cells of the table head.
		 */
		public function set headValue(variable:*):void
		{
			AComponent(getElementAt(0)).value = variable;
		}
		public function get headValue():*
		{
			return AComponent(getElementAt(0)).value;
		}
		/**
		 * Get or set a string array that indicates the lables of cells of the table body.
		 */
		public function set bodyValue(variable:*):void
		{
			setValue(variable,1);
		}
		public function get bodyValue():*
		{
			var len:int = numElements - 1;
			var items:Array = new Array(len);
			for(var i:int = 0;i < len;++i)
			{
				items[i] = AComponent(getElementAt(i + 1)).value;
			}
			return items;
		}
		/**
		 * Set value.
		 */
		protected function setValue(variable:*,startIndex:int):void
		{
			var items:Array = variable as Array;
			var cur:int = numElements > startIndex ? numElements - startIndex : 0;
			var end:int = items ? items.length : 0;
			// append item
			while(cur < end)
			{
				elements.addChild(new Tr);
				++cur;
			}
			// delete item
			while(cur > end)
			{
				elements.removeChildAt(startIndex + end);
				--cur;
			}
			// update item's value
			for(var i:int = 0;i < end;++i)
			{
				var item:AComponent = AComponent(getElementAt(i + startIndex));
				item.value = items[i];
			}
			// update
			updateAlignment(startIndex);
			updateContentBox();
		}
		/**
		 * Realign cell.
		 */
		public function realignCell():void
		{
			var col:int;
			var colLen:int;
			var colItem:DisplayObject;
			var row:int;
			var rowLen:int;
			var rowItem:Tr;
			
			//columnsWidth.length = this.getElementAt(0).numElements;
			for(row = 0,rowLen = numElements;row < rowLen;++row)
			{
				rowItem = Tr(getElementAt(row));
				for(col = 0,colLen = rowItem.numElements;col < colLen;++col)
				{
					colItem = rowItem.getElementAt(col);
					colItem.width = columnsWidth[col];
				}
				rowItem.updateAlignment();
				rowItem.updateContentBox();
			}
		}
		/**
		 * @private
		 */
		override protected function resizeBoxModel():void
		{
			super.resizeBoxModel();
			// update rows' width
			for(var i:int = 0,len:int = numElements;i < len;++i)
			{
				var unit:DisplayObject = this.getElementAt(i);
				unit.width = boxModel.content.width;
			}
		}
	}
}